package com.fengxing.ams.web.facade.mouble.user;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fengxing.ams.pub.util.Collections3;
import com.fengxing.ams.web.facade.mouble.college.CollegeDetailVO;
import com.fengxing.ams.web.facade.mouble.resource.UserResourceVO;
import com.google.common.collect.Lists;

@Entity
@Table(name = "zyt_sys_user")
@JsonIgnoreProperties(value={"roleList", "mobile", "lastLoginIp", "lastLoginDate", "password", "userResourceList"}, ignoreUnknown = true)
public class UserVO {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long userId;
	private String name;
	private String email;
	private String password;
	private String photo;
	private String mobile;
	private Integer status;
	private Date createDate;
	private Date lastLoginDate;
	private String lastLoginIp;
	private Integer score;
	private String gendar;
	private String subject;
	private Date update_date;
	private Integer collegeId;
	
	public Integer getCollegeId() {
		return collegeId;
	}
	public void setCollegeId(Integer collegeId) {
		this.collegeId = collegeId;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "zyt_sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") })
	@OrderBy("role_id") @Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	private List<RoleVO> roleList;
	
	public String getPhoto() {
		return photo;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}

	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}

	public void setScore(Integer score) {
		this.score = score;
	}

	public void setGendar(String gendar) {
		this.gendar = gendar;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setUpdate_date(Date update_date) {
		this.update_date = update_date;
	}

	public void setRoleList(List<RoleVO> roleList) {
		this.roleList = roleList;
	}

	public Integer getScore() {
		return score;
	}

	public String getGendar() {
		return gendar;
	}

	public String getSubject() {
		return subject;
	}

	public Date getUpdate_date() {
		return update_date;
	}

	public Long getUserId() {
		return userId;
	}

	public String getName() {
		return name;
	}

	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getMobile() {
		return mobile;
	}

	public Integer getStatus() {
		return status;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public Date getLastLoginDate() {
		return lastLoginDate;
	}

	public String getLastLoginIp() {
		return lastLoginIp;
	}

	public List<RoleVO> getRoleList() {
		if (null == roleList) {
			roleList = Lists.newArrayList();
		}
		return roleList;
	}
	
	
	@Transient
	public List<Long> getRoleIdList() {
		List<Long> roleIdList = Lists.newArrayList();
		for (RoleVO role : roleList) {
			roleIdList.add(role.getRole_id());
		}
		return roleIdList;
	}

	@Transient
	public void setRoleIdList(List<Long> roleIdList) {
		roleList = Lists.newArrayList();
		for (Long roleId : roleIdList) {
			RoleVO role = new RoleVO();
			role.setRole_id(roleId);
			roleList.add(role);
		}
	}

	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	@Transient
	public String getRoleNames() {
		return Collections3.extractToString(roleList, "name", ", ");
	}
	
	@PrePersist
	public void prePersist() {
		this.setCreateDate(new Date());
		this.setGendar("男");
		this.setStatus(0);
    }
}
